package com.thesis.manage.common.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.thesis.manage.common.constant.YesOrNoEnum;
import com.thesis.manage.common.entity.AbstractBaseEntity;
import com.thesis.manage.common.service.BaseService;
import org.springframework.data.domain.Pageable;
import org.springframework.util.Assert;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * @author php
 * @date 2018/2/1
 */
public abstract class BaseServiceImpl<T extends AbstractBaseEntity> implements BaseService<T> {
    /**
     * 获取mapper对象
     *
     * @return mapper
     */
    protected abstract Mapper<T> getMapper();

    /**
     * 获取类型
     *
     * @return class
     */
    protected abstract Class<T> getEntityClass();


    @Override
    public PageInfo<T> list(final Pageable pageable) {
        if (pageable == null) {
            PageHelper.startPage(1, 10);
        } else {
            PageHelper.startPage(pageable.getPageNumber(), pageable.getPageSize());
        }
        Example example = new Example(getEntityClass());
        example.orderBy("startTime").desc();
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("delStatus", YesOrNoEnum.NO.getValue());
        List<T> list = this.getMapper().selectByExample(example);
        return new PageInfo<>(list);
    }

    @Override
    public void delete(Long id) {
        T one = this.findById(id);
        Assert.notNull(one, "数据不存在");
        try {
            T o = getEntityClass().newInstance();
            o.setDelStatus(YesOrNoEnum.YES.getValue());
            o.setId(id);
            o.setEndTime(new Date());
            this.getMapper().updateByPrimaryKeySelective(o);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public T findById(Long id) {
        Assert.notNull(id, "id不能为空");
        try {
            T o = getEntityClass().newInstance();
            o.setDelStatus(YesOrNoEnum.NO.getValue());
            o.setId(id);
            return this.getMapper().selectOne(o);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public T findByOne(T t) {
        Assert.notNull(t, "对象未实例化");
        t.setDelStatus(YesOrNoEnum.NO.getValue());
        return this.getMapper().selectOne(t);
    }

    @Override
    public Integer save(T t) {
        Assert.notNull(t, "对象未实例化");
        if (t.getId() != null) {
            t.setEndTime(new Date());
            this.getMapper().updateByPrimaryKeySelective(t);
        } else {
            t.setStartTime(new Date());
            t.setEndTime(t.getStartTime());
            t.setDelStatus(YesOrNoEnum.NO.getValue());
            this.getMapper().insert(t);
        }
        return t.getId().intValue();
    }

    @Override
    public PageInfo<T> list(T t, Pageable pageable) {
        if (pageable == null) {
            PageHelper.startPage(1, 10);
        } else {
            PageHelper.startPage(pageable.getPageNumber(), pageable.getPageSize());
        }
        t.setDelStatus(YesOrNoEnum.NO.getValue());
        List<T> list = this.getMapper().select(t);
        return new PageInfo<>(list);
    }
}
